package com.cloudfun.msglisten.ws.haier.handler;

import com.alibaba.fastjson.JSONObject;
import com.cloudfun.msglisten.common.constant.RabbitConstants;
import com.cloudfun.msgsubscribe.DeviceInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * Created by Huqin on 2020/3/30
 */
@Slf4j
@Component
public class DeviceFirmwareMsgHandler extends AbstractDeviceMsgHandler {

    @Override
    public boolean canHandle(String topicName) {
        return "deviceInfo".equals(topicName);
    }

    @Override
    protected void doHandle(JSONObject jsonObject, DeviceInfo deviceInfo, String appId) {
        String iccid = null;
        for (Object o : jsonObject.getJSONArray("deviceModules")) {
            JSONObject module = (JSONObject) o;
            for (Object moduleInfo : module.getJSONArray("moduleInfos")) {
                JSONObject info = (JSONObject) moduleInfo;
                if (info.getString("key").equals("iccid")) {
                    iccid = info.getString("value");
                }
            }
        }
        deviceInfo.setAttr("iccid", iccid);
        // 内部广播
        log.info("2G设备消息：{}发送到外部：{}", deviceInfo, appId);
        rabbitTemplate.convertAndSend(RabbitConstants.Exchange.DEVICE_FIRMWARE, "", deviceInfo);
    }

}
